package com.autosalon.ui.automobil.action;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.ui.automobil.Automobil;
import com.autosalon.ui.automobil.state.AutomobilStateHolder;
import com.vaadin.ui.Button.ClickEvent;

/**
 *  Trida slouzici k mazani autmobilu
 * @author Petr Kriegisch
 */

public class DeleteAutomobilAction extends AbstractAutomobilAction {

    com.autosalon.model.Automobil vybranyAutomobil;

    public DeleteAutomobilAction(Automobil automobil) {
        super(automobil);
    }

    @Override
    public void buttonClick(ClickEvent event) {
        int id;
        vybranyAutomobil = (com.autosalon.model.Automobil) DaoFactory.getinstance().getAutomobil().getById(Long.parseLong(event.getButton().getData().toString()));
        try {
            DaoFactory.getinstance().getAutomobil().delete(vybranyAutomobil);
            id = automobil.getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
            automobil.getWindow().showNotification("Automobil smazan");
            automobil.naplneniVsechTabulek();
            AutomobilStateHolder.setState(AutomobilStateHolder.VIEW_AUTOMOBIL_STATE, id, super.automobil);
        } catch (NullPointerException e) {
            id = 1;
            automobil.getWindow().showNotification("Smazani neprobehlo korektne");
        }


    }
}
